const express = require('express')
const app = express()
const server = require('http').createServer(app)

// 品牌列表
const brand = require('./data/brand.json')
// 商品列表
const goods = require('./data/goods.json')
// 收货地址
const address = require('./data/address.json')
// 购物车
const shopcart = require('./data/shopcart.json')
// 订单
const orders = require('./data/order.json')
// 商品评价
const evaluate = require('./data/evaluate.json')
server.listen(7001)

console.log('fashionClothes Server Listening 7001')

app.use(express.static(__dirname + '/ClothesApp'))

// 入口
app.get('/', (req, res) => {
  res.send(goods)
})

// 查询所有品牌信息
app.get('/brandList', (req, res) => {
  res.send(brand)
})

// 查询某品牌下的商品信息
app.get('/goodsByBrand/:brandId', (req, res) => {
  res.send(goods.filter((value) =>
    value.id == req.params.brandId
  ))
})

// 根据商品类型查询商品信息
app.get('/goodsByType/:type', (req, res) => {
  const arr = []
  for (let i = 0; i < goods.length; i++) {
    let list = goods[i].productList
    for (let j = 0; j < list.length; j++) {
      if (list[j].type === req.params.type) {
        arr.push(list[j])
      }
    }
  }
  res.send(arr)
})

// 查询所有收货地址
app.get('/address/:userid', (req, res) => {
  const userid = req.params.userid
  for (let i of address) {
    if (i.userid == userid) {
      res.send(i.address)
      return
    }
  }
  res.send([])
})

// 新增收货地址
app.post('/addAddress', (req, res) => {
  const userid = req.body.userid
  for (let i of address) {
    if (i.userid == userid) {
      const last = i.address[i.address.length - 1]
      const obj = {
        id: last.id + 1,
        name: req.body.name,
        phone: req.body.phone,
        content: req.body.content
      }
      i.address.unshift(obj)
      res.send('success')
      return
    }
  }
  res.send('fail')
})

// 删除收货地址
app.post('/deleteAddress', (req, res) => {
  const userid = req.body.userid
  for (let i of address) {
    if (i.userid == userid) {
      i.address.splice(req.body.id, 1)
      res.send('success')
      return
    }
  }
  res.send('fail')
})

// 添加到购物车
app.post('/addProductToShopCart', (req, res) => {
  const userid = req.body.userid
  for (let i of shopcart) {
    if (i.userid == userid) {
      const last = i.shopcart[i.shopcart.length - 1]
      const obj = {
        id: last.id + 1,
        productId: req.body.productId,
        logo: req.body.logo,
        price: req.body.price,
        count: req.body.count
      }
      i.shopcart.unshift(obj)
      res.send('success')
      return
    }
  }
  res.send('fail')
})

// 根据状态类型查询订单数据(待付款,待收货,待发货,待评价)
app.get('/orders/:status', (req, res) => {
  res.send(
    orders.filter(value =>
      value.status === req.params.status
    )
  )
})

// 新增订单
app.post('/addOrder', (req, res) => {
  const last = orders[orders.length - 1]
  const obj = {
    id: last.id + 1,
    productId: req.body.productId,
    productName: req.body.productName,
    money: req.body.money,
    userid: req.body.userid,
    username: req.body.username,
    status: 1,
    count: req.body.count
  }
})

// 根据商品ID查找所有评价
app.get('/evaluate/:productId', (req, res) => {
  const productId = req.params.productId
  res.send(evaluate.filter(value => 
    value.productId === productId
  ))
})